//
//  Common.h
//  sudic
//
//  Created by linqc on 16/9/16.
//  Copyright © 2016年 linqc. All rights reserved.
//

#ifndef Common_h
#define Common_h

#import <UIKit/UIKit.h>
#import <objc/runtime.h>



@interface Common : NSObject
+ (UIColor *) colorWithHexString: (NSString *) stringToConvert;
+(NSString* _Nonnull)getWrittenFilePath:(NSString* _Nonnull)fileName;
+(NSString* _Nonnull)getWrittenFileDir;

+ (id)objectWithJsonString:(NSString *_Nullable)jsonString ;
+ (NSString*_Nullable)objectToJson:(id)dic;


#define INVLIAD_CONSTRAINT_VALUE 999999
+ (void)addLayoutConstraintsWithViewController:(UIViewController *)viewController
                                   contentView:(UIView *)contentView
                                          left:(NSInteger)leftSpace
                                           top:(NSInteger)topSpace
                                         right:(NSInteger)rightSpace
                                        bottom:(NSInteger)bottomSpace
                                        width:(NSInteger)widthSpace
                                        height:(NSInteger)heightSpace
                                        hasNav:(BOOL)hasNavigation;
+ (void)addLayoutConstraintsWithParentView:(UIView *)parentView
                                   contentView:(UIView *)contentView
                                          left:(float)leftSpace
                                           top:(float)topSpace
                                         right:(float)rightSpace
                                        bottom:(float)bottomSpace
                                         width:(float)widthSpace
                                    height:(float)heightSpace;
+(void)shareFile:(NSString *)path inVc:(UIViewController*)vc;
+(void)shareUrl:(NSString *)urlStr inVc:(UIViewController*)vc;


#define IS_PAD (UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPad)

#define DELAY_EXECUTE(delayTime,func) (dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{func;}))


#define AUTO_ADJUST_UI_ZERO(ROOT_VIEW) AUTO_ADJUST_UI(ROOT_VIEW,0,0,0,0)

#define AUTO_ADJUST_UI(ROOT_VIEW,theLeft,theTop,theRight,theBottom) \
AUTO_ADJUST_UI_NO_IPAD(ROOT_VIEW,theLeft,theTop,theRight,theBottom); \
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration \
{ \
[self ____auto_adjust_ui_core]; \
}

#define AUTO_ADJUST_UI_NO_IPAD(ROOT_VIEW,theLeft,theTop,theRight,theBottom) \
-(void)____auto_adjust_ui_core_main { \
if (@available(iOS 11.0, *)) { \
int topSpace = self.view.safeAreaInsets.top; \
int bottomSpace = self.view.safeAreaInsets.bottom; \
topSpace += theTop; \
bottomSpace += theBottom; \
if (theBottom == -1) bottomSpace = -self.view.safeAreaInsets.bottom; \
CGRect viewFrame = self.view.frame; \
CGRect tempFrame; \
tempFrame.origin.x = theLeft; \
tempFrame.origin.y = topSpace; \
tempFrame.size.width = viewFrame.size.width - theLeft - theRight; \
tempFrame.size.height = viewFrame.size.height - topSpace - bottomSpace; \
if (tempFrame.size.width != ROOT_VIEW.frame.size.width || tempFrame.size.height != ROOT_VIEW.frame.size.height) \
ROOT_VIEW.frame = tempFrame; \
} \
} \
-(void)____auto_adjust_ui_core { \
if (@available(iOS 11.0, *)) { \
if (IS_PAD) { \
dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)); \
dispatch_after(delayTime, dispatch_get_main_queue(), ^{ \
[self ____auto_adjust_ui_core_main]; \
}); \
} else { \
[self ____auto_adjust_ui_core]; \
} \
} \
} \
- (void)viewSafeAreaInsetsDidChange { \
[super viewSafeAreaInsetsDidChange]; \
[self ____auto_adjust_ui_core_main]; \
}

#define AUTO_ADJUST_UI_ZERO_PRE11(ROOT_VIEW) AUTO_ADJUST_UI_PRE11(ROOT_VIEW,0,0,0,0)

#define AUTO_ADJUST_UI_PRE11(ROOT_VIEW,theLeft,theTop,theRight,theBottom) \
AUTO_ADJUST_UI_PRE11_WITH_NAVI(ROOT_VIEW,theLeft,theTop,theRight,theBottom,true)

#define AUTO_ADJUST_UI_PRE11_WITH_NAVI(ROOT_VIEW,theLeft,theTop,theRight,theBottom,hasNav) \
if (@available(iOS 11.0, *)) { \
} else { \
AUTO_ADJUST_UI_PRE11_CORE(ROOT_VIEW,theLeft,theTop,theRight,theBottom,hasNav); \
}

#define AUTO_ADJUST_UI_PRE11_CORE(ROOT_VIEW,theLeft,theTop,theRight,theBottom,hasNav) \
{ \
ROOT_VIEW.translatesAutoresizingMaskIntoConstraints = NO; \
NSString *VFL_H = @"H:|-hspace-[tempRootView]-hspace-|"; \
NSString *VFL_V = @"V:|-topspace-[tempRootView]-bottomspace-|"; \
CGFloat navigationHeight = 0; \
if (hasNav && self.navigationController && self.navigationController.navigationBar) \
navigationHeight = self.navigationController.navigationBar.frame.size.height; \
int topSpace = navigationHeight + [[UIApplication sharedApplication] statusBarFrame].size.height + theTop; \
NSDictionary *metrics = @{ \
@"hspace" : @(theLeft), \
@"topspace":@(topSpace), \
@"bottomspace":@(theBottom) \
}; \
NSDictionary *views = @{@"tempRootView" : ROOT_VIEW}; \
NSArray *arrayH = [NSLayoutConstraint constraintsWithVisualFormat:VFL_H options:NSLayoutFormatAlignAllTop metrics:metrics views:views]; \
NSArray *arrayV = [NSLayoutConstraint constraintsWithVisualFormat:VFL_V options:kNilOptions metrics:metrics views:views]; \
[self.view addConstraints:arrayV]; \
[self.view addConstraints:arrayH]; \
}

@end


#endif /* Common_h */
